package spring.fox.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.SmartLifecycle;
import spring.fox.context.SpringFoxRpcApplicationContext;

/**
 * @author cuichao
 * Spring-Application 生命周期函数
 */
public class RpcApplicationLifeCycle implements SmartLifecycle , ApplicationContextAware {

    private ApplicationContext context;

    @Override
    public void start() {
        SpringFoxRpcApplicationContext rpcContext = context.getBean(SpringFoxRpcApplicationContext.class);
        try {
            rpcContext.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        SpringFoxRpcApplicationContext rpcContext = context.getBean(SpringFoxRpcApplicationContext.class);
        try {
            rpcContext.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isRunning() {
        return false;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }


}
